package com.sq.partner.manager.controller;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sq.partner.manager.common.Constants;
import com.sq.partner.manager.common.Result;
import com.sq.partner.manager.mybatis.model.GatewayPolicy;
import com.sq.partner.manager.mybatis.model.GatewaySubChannel;
import com.sq.partner.manager.mybatis.model.GatewayUser;
import com.sq.partner.manager.mybatis.model.TradCheckcontacts;
import com.sq.partner.manager.mybatis.model.TradCreditLmtDef;
import com.sq.partner.manager.service.IGatewayLifePolicyListService;
import com.sq.partner.manager.service.TradCheckcontactsService;
import com.sq.partner.manager.util.LogUtil;
import com.sq.partner.manager.util.Page;
import com.sq.partner.manager.util.SqlUtil;
import com.sq.partner.manager.util.StrUtil;

/**
 * 对账联系人管理
 * @author sankun
 *
 */
@Controller
@RequestMapping(value="/dataLinkManage")
public class DataLinkManageController {
	private static Logger logger = LogUtil.getLog(DataLinkManageController.class);
	
	@Resource
	private IGatewayLifePolicyListService lifePolicyListService;
	@Resource
	private TradCheckcontactsService tradCheckcontactsService;
	/**
	 * 跳转界面
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/dataLinkManage")
    public String dataLinkManage() throws Exception {
		return "transManager/dataLinkManage";
    }

	/**
	 * 查询当前用户的权限机构
	 * @param session
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/queryPrivilegeBranch",method = RequestMethod.POST)
	@ResponseBody
    public Result queryPrivilegeBranch(HttpSession session) throws Exception {
		GatewayUser user=(GatewayUser) session.getAttribute(Constants.SESSION_USER);
		logger.info("当前登录用户的userCode为"+user.getUserCode()+",DepartmentStr:"+user.getDepartmentStr());
		
		if(null != user && StrUtil.isNotEmpty(user.getDepartmentStr())){
			return Result.data(lifePolicyListService.queryPrivilegeBranch(user.getDepartmentStr()));
		}
		
		return Result.fail();
    }

	/**
	 * 分页条件查询人险保单
	 * @throws Exception
	 */
	@RequestMapping(value = "/queryTradCheckPageByCriteria")
	@ResponseBody
    public Page queryTradCheckPageByCriteria(HttpSession session, Integer pageIndex,String privilegeBranch,String partnerNo,String email,Page<Map<String,Object>> page) throws Exception {
		Map<String,Object> paramMap = SqlUtil.buildPageMap(page);
		if(StrUtil.isEmpty(privilegeBranch)){
			privilegeBranch=null;
		}else{
			privilegeBranch=privilegeBranch+"%";
		}
		paramMap.put("deptCode", privilegeBranch);
		if(StrUtil.isEmpty(partnerNo)){
			partnerNo=null;
		}
		if(StrUtil.isEmpty(email)){
			email=null;
		}else{
			email="%"+email+"%";
		}
		paramMap.put("partnerNo", partnerNo);
		paramMap.put("email",email);
		page.setMap(paramMap);
		return tradCheckcontactsService.queryCheckcontactsPageByCriteria(page);
    }
	
	@RequestMapping("/saveOrUpdateTradCheck")
	@ResponseBody
	public Result saveOrUpdateTradCheck(String flag, String partnerNo,String receivEmail,String copyEmail,String partnerName,String deptCode) throws Exception{
		if(StrUtil.isEmpty(partnerNo)){
			return Result.error("合作伙伴不能为空，请选择合作伙伴！");
		}
		if(StrUtil.isEmpty(receivEmail)){
			return Result.error("收件人邮箱地址为空！");
		}
		if(StrUtil.isEmpty(deptCode)){
			return Result.error("机构代码为空");
		}
		TradCheckcontacts tradCheck=new TradCheckcontacts();
		tradCheck.setChannelCode(partnerNo);
		tradCheck.setChannelName(partnerName);
		tradCheck.setCopyEmail(copyEmail);
		tradCheck.setReceivEmail(receivEmail);
		tradCheck.setDeptCode(deptCode);
		try {
			if("add".equals(flag)){
				return tradCheckcontactsService.addCheckcontacts(tradCheck);
			}else{
				return tradCheckcontactsService.editCheckcontacts(tradCheck);
			}
		} catch (Exception e) {
			logger.info("操作失败",e);
			return Result.error("操作失败");
		}
		
	}
	@RequestMapping("/deleteCheckcontacts")
	@ResponseBody
	public Result deleteCheckcontacts( String partnerNo,String deptCode) throws Exception{
		if(StrUtil.isEmpty(partnerNo)){
			return Result.error("合作伙伴不能为空，请选择合作伙伴！");
		}
		if(StrUtil.isEmpty(deptCode)){
			return Result.error("机构代码为空");
		}
		TradCheckcontacts tradCheck=new TradCheckcontacts();
		tradCheck.setChannelCode(partnerNo);
		tradCheck.setDeptCode(deptCode);
		try {
			return tradCheckcontactsService.deleteCheckcontacts(tradCheck);
		} catch (Exception e) {
			logger.info("操作失败",e);
			return Result.error("操作失败");
		}
	}
}
